﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using Users;
using Users.SnsConnect;

namespace WanPiJie.AuthSite.Controllers
{
	public class SnsAuthController : Controller
	{
		//
		// GET: /SnsAuth/

		public ActionResult Index(string sns)
		{
			sns = sns.ToLower();
			string authUrl = "";
			switch (sns)
			{
				case "taobao":
					authUrl = OAuth2Utils.CreateOAuth2Client(OAuth2Utils.Taobao_OAuth2Params).GetFullAuthUrl();
					break;
				case "weibo":
					authUrl = OAuth2Utils.CreateOAuth2Client(OAuth2Utils.Weibo_OAuth2Params).GetFullAuthUrl();
					break;
				case "qzone":
					authUrl = OAuth2Utils.CreateOAuth2Client(OAuth2Utils.Qzone_OAuth2Params).GetFullAuthUrl();
					break;
				case "renren":
					authUrl = OAuth2Utils.CreateOAuth2Client(OAuth2Utils.Renren_OAuth2Params).GetFullAuthUrl();
					break;
				default:
					return View();
			}
			return this.Redirect(authUrl);
		}

		//
		// GET: /SnsAuth/TabaoCallback

		public ActionResult TabaoCallback(string sns, string code, string error)
		{


			return View();
		}

		//
		// GET: /SnsAuth/WeiboCallback

		public ActionResult WeiboCallback(string sns, string code, string error)
		{
			OAuth2Client oAuth2Client = OAuth2Utils.CreateOAuth2Client(OAuth2Utils.Weibo_OAuth2Params, code);
			string json = oAuth2Client.ExchangeAccessToken();
			WeiboToken token = JsonConvert.DeserializeObject<WeiboToken>(json);
			string url = "https://api.weibo.com/2/users/show.json?access_token=" + token.access_token + "&uid=" + token.uid;
			string userJson = Itfort.Net.HttpClient.RequestContent(new Uri(url), "GET", Encoding.UTF8);
			WeiboUserInfo wbUser = JsonConvert.DeserializeObject<WeiboUserInfo>(userJson);

			return View();
		}

		//
		// GET: /SnsAuth/QzoneCallback

		public ActionResult QzoneCallback(string sns, string code, string error)
		{


			return View();
		}

		
	}
}
